#include "ul_single_pop_stack.h"
#include "ul_malloc.h"
#include "ul_atomic.h"
using namespace LinuxNetworkUtility;
LinuxNetworkUtility::UlSinglePopStack::UlSinglePopStack()
{
  head_ = NULL;
}

LinuxNetworkUtility::UlSinglePopStack::~UlSinglePopStack()
{
  head_ = NULL;
}


void LinuxNetworkUtility::UlSinglePopStack::push_tsk(TskHandle *hdl)
{
  SLink *first = NULL;
  if (NULL != hdl)
  {
    do
    {
      first = reinterpret_cast<SLink*>(head_);
      hdl->link_.insert(first);
    }while (atomic_compare_exchange_pointer(&head_,hdl,first) != first);
  }
}

LinuxNetworkUtility::UlSinglePopStack::TskHandle *LinuxNetworkUtility::UlSinglePopStack::pop_tsk()
{
  volatile SLink *first = NULL;
  do
  {
    first = reinterpret_cast<SLink*>(head_);
  }while (NULL != first 
          && atomic_compare_exchange_pointer(&head_,((SLink*)first)->next(),(void*)first) != first);
  return reinterpret_cast<TskHandle*>((void*)first);
}
